package  
{
	import flash.display.Scene;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.events.EventDispatcher;
	import flash.ui.Keyboard;
	import view.monsters.*;
	import view.ui.*;
	import view.player.*;
	import view.npc.*;
	import model.*;
	
	/**
	 * ...
	 * @author 
	 */
	public class View extends EventDispatcher 
	{
		public static var STATE_STOP:int = 0;
		public static var STATE_BEHIT:int = 2;
		public static var STATE_DEAD:int = 3;
		public var bBehit:Boolean = false;//被攻击
		public var bDead:Boolean = false;//是否死亡
		
		private static var _instance:View;
		public var viewStage:Sprite;
		public var scenes:Scenes;
		public var m_Player:Player;
		public var m_aMonsters:Array;
		public var m_aMyNPCs:Array;
		public var m_UI:UI;
		
		private var NumMonsters:int = 15;//先随意设置一个值用于测试
		private var NumPlayers:int = 1;//玩家最多两个，这里先设置为1个
		private var NumNPCs:int = 1;//NPC个数
		private var playerCount:int = 3;
		private var money:int;
		//以下两个变量记录游戏第几关，该关卡记几部分
		private var level:int;
		private var section:int;
		
		public function View() 
		{
			_instance = this;
			money = 0;
			viewStage = new Sprite(); 
		}
		//返回一个View单例
		public static function GetInstance():View {
			if (_instance)
				return _instance;
			else
				return new View();
		}
		//返回viewStage的舞台
		public function get getStage():Stage {
			return this.viewStage.stage;
		}
		//重置场景、玩家、NPC等信息
		public function resetData():void {
			//重置场景信息
			if (scenes)
				this.viewStage.removeChild(scenes);
			else {
				scenes = new Scenes(1,1)
				this.viewStage.addChild(scenes)
				this.viewStage.setChildIndex(scenes,0);
			}
			var scencsData:ScencsData = new ScencsData();
			scencsData.leftLimit = 0;
			scencsData.rightLimit = 800;
			scencsData.startX = 50;
			scencsData.startY = 450;
			//重置NPC
			m_aMyNPCs = new Array();
			for (i = 0; i < NumNPCs; i++)
			{
				var npc:NPC1 = new NPC1();
				npc.scaleX = 1.5;
				npc.scaleY = 1.5;
				m_aMyNPCs.push(npc);
				this.viewStage.addChild(npc);
			}
			//重置玩家信息
			if (m_Player)
				this.viewStage.removeChild(m_Player);
			else {
				m_Player = new Player();
				this.viewStage.addChild(m_Player);
			}
			//重置怪物信息。随机产生一个数，不同的数产生不用的怪
			m_aMonsters = new Array();
			var monster:Monster = new Monster();
			for (var i:int = 0; i < NumMonsters; i++)
			{
				var type:int = int(Math.random() * 4);
				switch (type)
				{
					case 0 :
						monster = new FlowerMonster();
						break;
					case 1 :
						monster = new Songshu();
						break;
					case 2 :
						monster = new Egg();
						break;
					case 3:
						monster = new Mushroom();
						break;
					case 4:
						monster = new Stone();
						break;
					default :
						break;
				}
				m_aMonsters.push(monster);
				this.viewStage.addChild(m_aMonsters[i]);
				monster.addEventListener("death", destroy);
			}
			//重置UI
			if (m_UI)
				this.viewStage.removeChild(m_UI);
			else {
				m_UI = new UI()
				this.viewStage.addChild(m_UI)
				this.viewStage.setChildIndex(m_UI,this.viewStage.numChildren-1);
			}
			
			setPos();
		}
		
		//设置怪物和NPC的分布位置
		private function setPos():void
		{
			for (var i:int = 0; i < m_aMonsters.length/3; i++)
			{
				m_aMonsters[i].setPos(750);
			}
			for (i = m_aMonsters.length/3; i < 2*m_aMonsters.length/3; i++)
			{
				m_aMonsters[i].setPos(1200);
			}
			for (i = 2*m_aMonsters.length/3; i < m_aMonsters.length; i++)
			{
				m_aMonsters[i].setPos(1700);
			}
			for (i = 0; i < m_aMyNPCs.length; i++)
			{
				m_aMyNPCs[i].setInitPos(300,400);
			}
		}
		
		//设置怪物和玩家的层次关系、更新镜头
		public function Logic(keyCode:Array):void
		{
			orderChilds();
			setView(keyCode);
			m_UI.updatUI();
		}
		
		//“摄像机”跟随效果
		private var bLeft:Boolean = false;
		private var bRight:Boolean = false;
		private function setView(keyCode:Array):void
		{
			var W:int = this.viewStage.stage.stageWidth / 2;
			var stageLimitX:int;
			var viewSpeed:int = 20;
			for each (var key:int in keyCode) {
				if (Keyboard.A == key) {
					bLeft = true;
					bRight = false;
					stageLimitX = W - m_Player.x + 150;
					break;
				}else if (Keyboard.D == key) {
					bRight = true;
					bLeft = false;
					stageLimitX = W - m_Player.x - 150;
					break;
				}else {
					bLeft = false;
					bRight = false;
					stageLimitX = W - m_Player.x;
					break;
				}
			}
			if (bLeft) {
				this.viewStage.x += viewSpeed;
				if (this.viewStage.x > stageLimitX) {
					//bLeft = false;
					this.viewStage.x = stageLimitX
				}
			}else if (bRight) {
				this.viewStage.x -= viewSpeed;
				if (this.viewStage.x < stageLimitX) {
					//bRight = false;
					this.viewStage.x = stageLimitX
				}
			}else {
				if (this.viewStage.x >= stageLimitX) {
					this.viewStage.x -= viewSpeed;
					if (this.viewStage.x < stageLimitX)
						this.viewStage.x = stageLimitX;
				}else if (this.viewStage.x < stageLimitX) {
					this.viewStage.x += viewSpeed;
					if (this.viewStage.x >= stageLimitX) {
						this.viewStage.x = stageLimitX;
					}
				}
			}
			
			if (this.viewStage.x > 0)
			{
				this.viewStage.x = 0;
			}
			if (this.viewStage.x < -this.viewStage.stage.stageWidth)
			{
				this.viewStage.x = -this.viewStage.stage.stageWidth;
			}
			//使背景的后部分保持在舞台中央
			scenes.ldrBgBack.x =  -this.viewStage.x;
			scenes.ldrBgBack.y =  -this.viewStage.y;
			m_UI.x = -this.viewStage.x;
			m_UI.y = -this.viewStage.y;
		}
		
		//怪物死亡时将怪物从数组和场景中移除
		private function destroy(e:Event):void{
			var temp:Monster = (Monster)(e.currentTarget)
			temp.visible = false;
			for(var i:int = m_aMonsters.length -1;i >= 0;i--){
				if(m_aMonsters[i] == temp){
					temp = m_aMonsters[m_aMonsters.length - 1];
					m_aMonsters[m_aMonsters.length - 1] = m_aMonsters[i];
					m_aMonsters[i] = temp;
					m_aMonsters.pop();
				}
			}
		}
		
		private function addMoney(e:Event):void {
		//	money += 10;
		//	m_UI.txtMoney.text = String(money);
		}
		
		//把player的文本读到TimeSaver里
		/*public function getTextLeft():TextField
		{
			return m_Player.m_TextLeft;
		}*/
		
		/*public function getTextRight():TextField
		{
			return m_Player.m_TextRight;
		}*/
		
		//设置怪物和玩家之间的层次关系，即谁挡住谁
		private function orderChilds():void
		{
			for(var i:int = 0;i< m_aMonsters.length;i++){
				var indexP:int = this.viewStage.getChildIndex(m_Player);
				var indexN:int = this.viewStage.getChildIndex(m_aMonsters[i]);
				if(indexP < indexN && m_Player.y > m_aMonsters[i].y ){
					this.viewStage.setChildIndex(m_aMonsters[i],indexP);
				}
				if(indexP > indexN && m_Player.y , m_aMonsters[i].y ){
					this.viewStage.setChildIndex(m_Player,indexN);
				}
			}
		}
		
		
	}

}